<template>
  <!-- 当点击 取消按钮 时，关闭弹窗 -->
  <div>
    <el-dialog
      :title="$t('mesProdOtherCode.reprint')"
      :visible.sync="visible"
      width="70%"
      top="4vh"
      :append-to-body="true"
      @close="clearDialog()"
      v-loading="loding"
      :close-on-click-modal="false"
    >
      <div>
        <ele-page
          :otherParams="otherParams"
          :formConfig="searchConfig"
          ref="eleTable"
          :operateList="operateList"
          @handleOperateEvent="handleOperateEvent"
          :column="column"
          :getList="mesProdOthercodeReprint"
        >
        </ele-page>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="clearDialog()">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
    <el-dialog
      width="20%"
      :title="$t('common.print')"
      :visible.sync="innerVisible"
      :append-to-body="true"
      @close="clearFormDialog()"
      v-loading="loding"
      :close-on-click-modal="false"
    >
      <el-form :model="form">
        <!-- <el-form-item :label="$t('mesProdOtherCode.printingTimes')" label-width="85px">
          <el-input size="small" disabled v-model="form.printingTimes"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearFormDialog()">{{
          $t("common.cancel")
        }}</el-button>
        <el-button type="primary" @click="handleRequest">{{
          $t("common.submit")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDict } from "@/api/common";
import { printTemplateInfoName } from "@/api/barcodeprint/mesBarcodeprintHistory";

import {
  mesProdOthercodeReprint,
  selectMesProdOthercodeReprintDetail,
} from "@/api/print/mesProdOthercode";
export default {
  name: "repairPrintTable",
  data() {
    let that = this;
    return {
      innerVisible: false,
      loding: false,
      printRow: {},
      form: { printingTimes: 0 },
      otherParams: { orderCode: "", codeType: "" },
      loding: false,
      visible: false,
      mesProdOthercodeReprint,
      operateList: [
        {
          name: $i18n.t("common.add"),
          type: "primary",
          icon: "el-icon-circle-plus-outline",
          handleName: "add", //点击时返回的标识
        },
      ],
      searchConfig: {
        inline: true,
        formDesc: {
          status: {
            layout: 7,
            type: "select",
            label: $i18n.t("common.status"),
            type: "select",
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              let res = await getDict("mes_barcode_reprint_status");
              return res.data.result || [];
            },
          },
          // isPrint: {
          //   layout: "6",
          //   type: "select",
          //   label: "是否打印",
          //   type: "select",
          //   prop: {
          //     value: "value",
          //     text: "description",
          //   },
          //   options: async (data) => {
          //     let res = await getDict("yes_or_not");
          //     return res.data.result || [];
          //   },
          // },
        },
      },
      column: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: $i18n.t("common.order"),
          width: 60,
          fixed: "left",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.orderCode"),
          prop: "orderCode",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.codeType"),
          prop: "codeType",
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict("mes_code_type");
            return list.data.result;
          },
        },
        {
          label: $i18n.t("mesOtherCodePrinting.startBarcode"), //"补打起始唯一码",
          prop: "startBarcode",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.endBarcode"),
          prop: "endBarcode",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.printTemp"), //"打印模板",
          prop: "printTemp",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.createByName"), //"申请人",
          prop: "createByName",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.createTime"), // "申请时间",11
          prop: "createTime",
        },
        {
          label: $i18n.t("common.status"), //"状态",
          prop: "status",
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            let res = await getDict("mes_barcode_reprint_status");
            return res.data.result || [];
          },
        },
        {
          label: $i18n.t("mesOtherCodePrinting.examineByName"), // "审核人",
          prop: "examineByName",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.examineTime"), // "审核时间",
          prop: "examineTime",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.examineContent"), // "审核内容",
          prop: "examineContent",
        },
        {
          label: $i18n.t("mesOtherCodePrinting.isPrint"), //"是否打印",
          prop: "isPrint",
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            let res = await getDict("yes_or_not");
            return res.data.result || [];
          },
        },
        {
          prop: "operate",
          label: $i18n.t("common.operation"),
          width: 160,
          fixed: "right",
          render: (h, { row }) => {
            let dom = <p></p>;
            if (row.status == "examinePass") {
              dom = (
                <el-button
                  onClick={() => {
                    that.print(row);
                  }}
                  type="primary"
                >
                  {$i18n.t("mesOtherCodePrinting.reprint")}
                </el-button>
              );
            }
            return dom;
          },
        },
      ],
      tableData: [],
      params: {},
    };
  },
  methods: {
    //初始化
    init(params) {
      console.log("2222" + params);
      this.params = params;
      this.otherParams = {
        isPrint: 0,
        orderCode: params.orderCode,
        codeType: params.codeType,
      };
      this.visible = true;
      this.refresh();
    },
    refresh() {
      this.$nextTick(() => {
        this.$refs.eleTable.refresh();
      });
    },

    clearDialog() {
      this.visible = false;
      this.$emit("refreshList");
    },
    // 表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name;
      const fnc = {
        add: () => {
          // this.$parent.  = true;
          this.$parent.$refs.repairPrintModel.init({
            ...this.params,
            barcodePrintTemp: this.params.barcodePrintTemp,
            quantity: 0,
            type: 1,
          });
        },
      };
      fnc[data.handleName]();
    },
    //点击打印按钮
    async handleRequest() {
      // if (this.params.printingTimes == 0) {
      //   this.$message.warning($i18n.t('printBoxCode.unableToPrint'))
      //   return
      // }
      let res = await printTemplateInfoName({
        templateName: this.printRow.printTemp,
      });

      let data = await selectMesProdOthercodeReprintDetail(this.printRow.id);
      let barcodeInfo = {
        ...res.data.result[0],
        values: data.data.result,
      };
      // ----条码打印多份（条码数量根据打印次数printingTimes重新计算）----
      let newInfoList = [];
      barcodeInfo.values.forEach((v) => {
        newInfoList = [
          ...newInfoList,
          ...Array(Number(this.params.printingTimes)).fill(v),
        ];
      });
      barcodeInfo.values = newInfoList;
      localStorage.splitPrint = JSON.stringify(barcodeInfo);
      this.visible = false;
      this.$parent.refresh(false);
      window.open("/mom/component/mes/splitPrint?type=10");
    },
    async print(row) {
      this.form.printingTimes = this.params.printingTimes;
      this.innerVisible = true;
      this.printRow = row;
    },
  },
};
</script>
